
package database;



import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import configs.ServerConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.console.Start;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



















 public class DatabaseConnection
         {
       public static final int RETURN_GENERATED_KEYS = 1;
    /*  24 */   private static final Logger log = LogManager.getLogger(DatabaseConnection.class);
    /*  25 */   private static final DruidDataSource druidDataSource = new DruidDataSource();
       private static final long connectionTimeOut = 300000L;
       private static String dbUrl;
    /*  28 */   private static DatabaseConnection databasePool = null;



    public static void init() {
        /*  31 */
        dbUrl = "jdbc:mysql://" + ServerConfig.DB_IP + ":" + ServerConfig.DB_PORT + "/" + ServerConfig.DB_NAME + "?autoReconnect=true&characterEncoding=GBK&zeroDateTimeBehavior=convertToNull&useSSL=true";

        try {
            /*  33 */
            druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
            /*  34 */
            druidDataSource.setUrl(dbUrl);
            /*  35 */
            druidDataSource.setUsername(ServerConfig.DB_USER);
            /*  36 */
            druidDataSource.setPassword(ServerConfig.DB_PASSWORD);
            /*  37 */
            druidDataSource.setFilters("log4j2");
            /*  38 */
            druidDataSource.setInitialSize(ServerConfig.DB_INITIALPOOLSIZE);
            /*  39 */
            druidDataSource.setMinIdle(ServerConfig.DB_MINPOOLSIZE);
            /*  40 */
            druidDataSource.setMaxActive(ServerConfig.DB_MAXPOOLSIZE);
            /*  41 */
            druidDataSource.setMaxWait(60000L);
            /*  42 */
            druidDataSource.setTimeBetweenEvictionRunsMillis(60000L);
            /*  43 */
            druidDataSource.setMinEvictableIdleTimeMillis(300000L);
            /*  44 */
            druidDataSource.setRemoveAbandoned(true);
            /*  45 */
            druidDataSource.setRemoveAbandonedTimeoutMillis(ServerConfig.DB_TIMEOUT);
            /*  46 */
            druidDataSource.setValidationQuery("SELECT 1 FROM dual");
            /*  47 */
            druidDataSource.setTestWhileIdle(true);
            /*  48 */
            druidDataSource.setTestOnBorrow(false);
            /*  49 */
            druidDataSource.setTestOnReturn(false);
            /*  50 */
            druidDataSource.setPoolPreparedStatements(true);
            /*  51 */
            druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
            /*  52 */
        } catch (Exception e) {
            /*  53 */
            log.error("[数据库信息] 初始化失败", e);
            /*  54 */
            System.exit(0);

        }

    }



    public static synchronized DatabaseConnection getInstance() {
        /*  59 */
        if (null == databasePool) {
            /*  60 */
            databasePool = new DatabaseConnection();

        }
        /*  62 */
        return databasePool;

    }



    private static long getWaitTimeout(Connection con) {
        /*  66 */
        try (Statement stmt = con.createStatement();
                /*  67 */         ResultSet rs = stmt.executeQuery("SHOW VARIABLES LIKE 'wait_timeout'")) {
            /*  68 */
            if (rs.next()) {
                /*  69 */
                return Math.max(1000, rs.getInt(2) * 1000 - 1000);

            }
            /*  71 */
            return -1L;


        }
        /*  74 */ catch (SQLException e) {
            /*  75 */
            return -1L;

        }

    }



    private static Connection connectToDB() {

        try {
            /*  81 */
            DruidPooledConnection druidPooledConnection = druidDataSource.getConnection();
            /*  82 */
            Long timeout = Long.valueOf(getWaitTimeout((Connection) druidPooledConnection));
            /*  83 */
            if (timeout.longValue() == -1L) {
                /*  84 */
                System.out.println("[数据库信息] 无法读取超时时间，using 300000 instead.");

            }
            /*  86 */
            return (Connection) druidPooledConnection;
            /*  87 */
        } catch (SQLException e) {
            /*  88 */
            log.error("[数据库信息] 数据库连接失败，请确认MYSQL服务是否开启，数据库名、账号、密码是否配置正确", e);
            /*  89 */
            throw new DatabaseException(e);

        }

    }



    private static void colse() {
        /*  94 */
        druidDataSource.close();

    }



    public static void closeAll() throws SQLException {
        /*  98 */
        druidDataSource.close();

    }

    public DataBaseStatus TestConnection() {

        DruidPooledConnection druidPooledConnection=null;

        DataBaseStatus ret;
        /* 102 */
        init();
        /* 103 */
        Connection localConnection = null;


        try {
            /* 106 */
            druidPooledConnection = druidDataSource.getConnection();
            /* 107 */
            ret = DataBaseStatus.连接成功;
            /* 108 */
        } catch (Exception e) {
            /* 109 */
            Start.showMessage("连接数据库失败", "错误", 1);
            /* 110 */
            System.exit(0);
            /* 111 */
            ret = DataBaseStatus.连接失败;

        } finally {

            try {
                /* 114 */
                if (druidPooledConnection != null) {
                    /* 115 */
                    druidPooledConnection.close();

                }
                /* 117 */
            } catch (SQLException e) {
                /* 118 */
                e.printStackTrace();

            }

        }
        /* 121 */
        return ret;

    }



    public DruidPooledConnection getConnection() throws SQLException {
        /* 125 */
        return druidDataSource.getConnection();

    }


       public enum DataBaseStatus {
        /* 129 */     未初始化,
        /* 130 */     连接成功,
        /* 131 */     连接失败;

    }


       public static class ConWrapper
             {
        /* 136 */     private long lastAccessTime = 0L;
             private Connection connection;



        public ConWrapper(Connection con) {
            /* 140 */
            this.connection = con;

        }



        public Connection getConnection() {
            /* 144 */
            if (expiredConnection()) {


                try {
                    /* 147 */
                    this.connection.close();
                    /* 148 */
                } catch (Throwable err) {
                    /* 149 */
                    throw new DatabaseException("数据库错误", err);

                }
                /* 151 */
                this.connection = DatabaseConnection.connectToDB();

            }
            /* 153 */
            this.lastAccessTime = System.currentTimeMillis();
            /* 154 */
            return this.connection;

        }








        public boolean expiredConnection() {
            /* 163 */
            if (this.lastAccessTime == 0L) {
                /* 164 */
                return false;

            }

            try {
                /* 167 */
                return (System.currentTimeMillis() - this.lastAccessTime >= 300000L || this.connection.isClosed());
                /* 168 */
            } catch (Throwable ex) {
                /* 169 */
                return true;

            }

        }

    }

}


/* Location:              C:\PGX\Server.jar!\database\DatabaseConnection.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */